package com.lishao.soccer.service.impl;

import java.math.BigDecimal;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpSession;

import com.lishao.soccer.bean.entity.SmUser;
import com.lishao.soccer.bean.entity.TmGameArrive;
import com.lishao.soccer.bean.entity.TmGameJudge;
import com.lishao.soccer.bean.entity.TmGameScore;
import com.lishao.soccer.bean.entity.TmGameSignup;
import com.lishao.soccer.bean.entity.TmGameTech;
import com.lishao.soccer.bean.entity.TmMember;
import com.lishao.soccer.bean.entity.TmTeamGame;
import com.lishao.soccer.bean.entity.TmTeamPlace;
import com.lishao.soccer.bean.form.TmTeamGameForm;
import com.lishao.soccer.dao.TmGameArriveMapper;
import com.lishao.soccer.dao.TmGameJudgeMapper;
import com.lishao.soccer.dao.TmGameScoreMapper;
import com.lishao.soccer.dao.TmGameSignupMapper;
import com.lishao.soccer.dao.TmGameTechMapper;
import com.lishao.soccer.dao.TmMemberMapper;
import com.lishao.soccer.dao.TmTeamGameMapper;
import com.lishao.soccer.dao.TmTeamPlaceMapper;
import com.lishao.soccer.service.TmTeamGameService;
import com.lishao.system.security.providers.SecurityUser;

public class TmTeamGameServiceImpl implements TmTeamGameService {
	TmTeamGameMapper tmTeamGameMapper;
	TmGameSignupMapper tmGameSignupMapper;
	TmTeamPlaceMapper tmTeamPlaceMapper;
	TmGameArriveMapper tmGameArriveMapper;
	TmGameTechMapper tmGameTechMapper;
	TmGameJudgeMapper tmGameJudgeMapper;
	TmGameScoreMapper tmGameScoreMapper;
	TmMemberMapper tmMemberMapper;
	public List <TmTeamGame> selectByTeam(Long teamId,Long userId){
		List <TmTeamGame> gameList = tmTeamGameMapper.selectByTeam(teamId);
		for(TmTeamGame tmpGame:gameList){
			Map paraMap = new HashMap();
			paraMap.put("teamId", teamId);paraMap.put("tmGameId", tmpGame.getRowId());paraMap.put("scoreUserId", userId);
			int iResult = tmGameJudgeMapper.countByMap(paraMap);
			if(iResult>0){
				tmpGame.setJudge(true);
			}else{
				tmpGame.setJudge(false);
			}
			int iBmrs = tmGameSignupMapper.countBmrsByMap(paraMap);
			tmpGame.setBmrs(Integer.valueOf(iBmrs));
			//设置管理员标志
			tmpGame.setManageFlag(getManageFlag(teamId, userId));
		}
		return gameList;
	}
	public boolean getManageFlag(Long teamId,Long userId){
    	boolean bResult=false;
    	Map paraMap = new HashMap();
    	paraMap.put("teamId", teamId);
    	paraMap.put("logUserId", userId);
    	List<TmMember> tmMemberList = tmMemberMapper.selectByTeamUser(paraMap);
    	if(tmMemberList!=null&&tmMemberList.size()>0){
    		TmMember tmMember = tmMemberList.get(0);
    		if("1".equals(tmMember.getManageFlag())){
    			bResult=true;
    		}
    	}
    	return bResult;
    }
	public List <TmTeamGame> selectActiveGameByTeam(Long teamId,Long userId){
		List <TmTeamGame> gameList = tmTeamGameMapper.selectActiveGameByTeam(teamId);
		for(TmTeamGame tmpGame:gameList){
			Map paraMap = new HashMap();
			paraMap.put("teamId", teamId);paraMap.put("tmGameId", tmpGame.getRowId());paraMap.put("scoreUserId", userId);
			int iResult = tmGameJudgeMapper.countByMap(paraMap);
			if(iResult>0){
				tmpGame.setJudge(true);
			}else{
				tmpGame.setJudge(false);
			}
			int iBmrs = tmGameSignupMapper.countBmrsByMap(paraMap);
			int iSignupNum = tmGameSignupMapper.countSignupNumByMap(paraMap);
			tmpGame.setBmrs(Integer.valueOf(iBmrs));
			tmpGame.setSignupNum(Integer.valueOf(iSignupNum));
			if(checkArriveRecord(teamId, tmpGame.getRowId(), userId)){
				tmpGame.setKqFlag("1");
			}else{
				tmpGame.setKqFlag("0");
			}
			//设置管理员标志
			tmpGame.setManageFlag(getManageFlag(teamId, userId));
		}
		return gameList;
	}
	
	public List <TmTeamGame> selectInActiveGameByTeam(Long teamId,Long userId){
		List <TmTeamGame> gameList = tmTeamGameMapper.selectInActiveGameByTeam(teamId);
		for(TmTeamGame tmpGame:gameList){
			Map paraMap = new HashMap();
			paraMap.put("teamId", teamId);paraMap.put("tmGameId", tmpGame.getRowId());paraMap.put("scoreUserId", userId);
			int iResult = tmGameJudgeMapper.countByMap(paraMap);
			if(iResult>0){
				tmpGame.setJudge(true);
			}else{
				tmpGame.setJudge(false);
			}
			int iBmrs = tmGameSignupMapper.countBmrsByMap(paraMap);
			int iSignupNum = tmGameSignupMapper.countSignupNumByMap(paraMap);
			tmpGame.setBmrs(Integer.valueOf(iBmrs));
			tmpGame.setSignupNum(Integer.valueOf(iSignupNum));
			if(checkArriveRecord(teamId, tmpGame.getRowId(), userId)){
				tmpGame.setKqFlag("1");
			}else{
				tmpGame.setKqFlag("0");
			}
			//设置管理员标志
			tmpGame.setManageFlag(getManageFlag(teamId, userId));
		}
		return gameList;
	}
	
	public TmTeamGame selectTeamGameById(Long tmGameId){
		return tmTeamGameMapper.selectByPrimaryKey(tmGameId);
	}
	
	public TmGameSignup selectGameSignup(Long teamId,Long tmGameId,Long signupUserId){
		TmGameSignup objSignup=null;
		Map paraMap = new HashMap();
		paraMap.put("teamId", teamId);
		paraMap.put("tmGameId", tmGameId);
		paraMap.put("signupUserId", signupUserId);
		List<TmGameSignup> signupList = tmGameSignupMapper.selectByMap(paraMap);
		if(signupList!=null&&signupList.size()>0){
			objSignup = signupList.get(0);
		}
		return objSignup;
	}
	
	public List<TmGameSignup> selectGameSignup(Long teamId,Long tmGameId){
		Map paraMap = new HashMap();
		paraMap.put("teamId", teamId);
		paraMap.put("tmGameId", tmGameId);
		List<TmGameSignup> signupList = tmGameSignupMapper.selectForView(paraMap);
		return signupList;
	}
	//报名
	public synchronized void gameSignUp(TmGameSignup tmGameSignup){
		//删除原先的报名
		Map paraMap = new HashMap();
		paraMap.put("tmGameId", tmGameSignup.getTmGameId());
		paraMap.put("signupUserId", tmGameSignup.getSignupUserId());
		tmGameSignupMapper.deleteMySignupByGame(paraMap);
		//产生新的报名
		tmGameSignupMapper.insertSelective(tmGameSignup);
	}
	
	public synchronized void gameKq(TmGameArrive tmGameArrive,HttpSession session){
		synchronized(session){
			boolean bChkResult = checkArriveRecord(tmGameArrive.getTeamId(), tmGameArrive.getTmGameId(), tmGameArrive.getArriveUserId());
			if(bChkResult){
				
			}else{
				tmGameArriveMapper.insertSelective(tmGameArrive);
			}
		}
	}
	
	public TmTeamPlace getTeamPlace(Long placeId){
		TmTeamPlace tmTeamPlace = tmTeamPlaceMapper.selectByPrimaryKey(placeId);
		return tmTeamPlace;
	}
	
	public List<TmTeamPlace> selectPlaceByTeamId(Long teamId){
		List<TmTeamPlace> placeList = tmTeamPlaceMapper.selectPlaceByTeamId(teamId);
		return placeList;
	}
	public List<TmGameTech> selectForGameTech(Long teamId,Long tmGameId,Long userId){
		Map paraMap = new HashMap();
		paraMap.put("teamId", teamId);
		paraMap.put("tmGameId", tmGameId);
		paraMap.put("userId", userId);
		List<TmGameTech> gameTechList = tmGameTechMapper.selectForGameTech(paraMap);
		for(TmGameTech tmpBean:gameTechList){
			tmpBean.setTmGameId(tmGameId);
		}
		return gameTechList;
	}
	
	//评分提交
	public void gameTechSubmit(Long teamId,Long tmGameId,SecurityUser secUser,List<TmGameTech> gameTechList){
		Map paraMap=new HashMap();
		paraMap.put("teamId", teamId);
		paraMap.put("tmGameId", tmGameId);
		paraMap.put("scoreUserId", secUser.getSmUser().getRowId());
		tmGameTechMapper.deleteSoccerTech(paraMap);
		
		for(int i=0;i<gameTechList.size();i++){
			TmGameTech tmpBean = gameTechList.get(i);
			tmpBean.setScoreUserId(secUser.getSmUser().getRowId());
			tmpBean.setDeletedFlag("0");
			tmGameTechMapper.insertSelective(tmpBean);
		}
	}
	
	//保存活动
	public boolean saveGame(TmTeamGameForm tmTeamGameForm,SmUser smUser) {
		boolean bResult;
		int iResult = 0;
		TmTeamGame tmTeamGame = new TmTeamGame();
		tmTeamGame.setRowId(tmTeamGameForm.getRowId());
		tmTeamGame.setTeamId(tmTeamGameForm.getTeamId());
		tmTeamGame.setGameName(tmTeamGameForm.getGameName());
		tmTeamGame.setGameSource(tmTeamGameForm.getGameSource());
		tmTeamGame.setGameType(tmTeamGameForm.getGameType());
		tmTeamGame.setScoredFlag(tmTeamGameForm.getScoredFlag());
		DateFormat df1 = new SimpleDateFormat("yyyy-MM-dd");
		DateFormat df2 = new SimpleDateFormat("yyyy-MM-dd HH:mm");
		try {
			tmTeamGame.setGameDate(df1.parse(tmTeamGameForm.getGameDate()));
			tmTeamGame.setStartTime(df2.parse(tmTeamGameForm.getGameDate()+" "+tmTeamGameForm.getStartTime()));
			tmTeamGame.setEndTime(df2.parse(tmTeamGameForm.getGameDate()+" "+tmTeamGameForm.getEndTime()));
			tmTeamGame.setArriveTime(df2.parse(tmTeamGameForm.getGameDate()+" "+tmTeamGameForm.getArriveTime()));
			tmTeamGame.setPunishTime(df2.parse(tmTeamGameForm.getGameDate()+" "+tmTeamGameForm.getPunishTime()));
			tmTeamGame.setSignupEndTime(df2.parse(tmTeamGameForm.getSignupEndTime()));
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		tmTeamGame.setStatusFlag("20");
		tmTeamGame.setPlaceId(tmTeamGameForm.getPlaceId());
		tmTeamGame.setPlaceName(tmTeamGameForm.getPlaceName());
		if(tmTeamGameForm.getPlaceId()>0){
			TmTeamPlace tmTeamPlace = tmTeamPlaceMapper.selectByPrimaryKey(tmTeamGameForm.getPlaceId());
			tmTeamGame.setPlaceLatitude(tmTeamPlace.getPlaceLatitude());
			tmTeamGame.setPlaceLongitude(tmTeamPlace.getPlaceLongitude());
		}else{
			tmTeamGame.setPlaceLatitude(BigDecimal.valueOf(0));
			tmTeamGame.setPlaceLongitude(BigDecimal.valueOf(0));
		}
		tmTeamGame.setOpponentTeamId(tmTeamGameForm.getOpponentTeamId());
		tmTeamGame.setOpponentName(tmTeamGameForm.getOpponentName());
		tmTeamGame.setOrganigerUserId(smUser.getRowId());
		if(tmTeamGame.getRowId()==null||tmTeamGame.getRowId()==-1){
			//新增保存
			iResult = tmTeamGameMapper.insertSelective(tmTeamGame);
		}else{
			//修改保存
			iResult = tmTeamGameMapper.updateByPrimaryKeySelective(tmTeamGame);
		}
		bResult=iResult==1?true:false;
		return bResult;
	}
	public TmTeamGame findGame(Long tmGameId){
		TmTeamGame tmTeamGame = tmTeamGameMapper.selectByPrimaryKey(tmGameId);
		return tmTeamGame;
	}
	public List<TmGameJudge> selectForJudgeAssign(Long teamId,Long tmGameId){
		Map paraMap = new HashMap();
		paraMap.put("teamId", teamId);
		paraMap.put("tmGameId", tmGameId);
		int iResult = tmGameJudgeMapper.countByMap(paraMap);
		List<TmGameJudge> gameJudgeList = null;
		if(iResult>0){
			gameJudgeList = tmGameJudgeMapper.selectForJudgeAssign(paraMap);
		}else{
			gameJudgeList = tmGameJudgeMapper.selectForJudgeAssignDefault(paraMap);
		}
		return gameJudgeList;
	}
	public List<TmGameJudge> selectForAddarrived(Long teamId,Long tmGameId){
		Map paraMap = new HashMap();
		paraMap.put("teamId", teamId);
		paraMap.put("tmGameId", tmGameId);
		List<TmGameJudge> gameJudgeList = tmGameJudgeMapper.selectForAddarrived(paraMap);
		return gameJudgeList;
	}
	public void gameJudgeAssignSubmit(Integer ourScore,Integer oppScore,Long teamId, Long tmGameId, SecurityUser secUser,List<TmGameJudge> gameJudgeList){
		//更新比分
		TmTeamGame tmTeamGame = new TmTeamGame();
		tmTeamGame.setRowId(tmGameId);tmTeamGame.setOurScore(ourScore);tmTeamGame.setOppScore(oppScore);
		tmTeamGameMapper.updateByPrimaryKeySelective(tmTeamGame);
		
		//删除之前指定的评委
		Map paraMap = new HashMap();
		paraMap.put("teamId", teamId);
		paraMap.put("tmGameId", tmGameId);
		tmGameJudgeMapper.deleteByGameId(paraMap);
		
		//评委
		for(TmGameJudge tmGameJudge:gameJudgeList){
			//更新是否上场标志
			paraMap = new HashMap();
			paraMap.put("playFlag", tmGameJudge.getPlayFlag());
			paraMap.put("teamId", teamId);
			paraMap.put("tmGameId", tmGameId);
			paraMap.put("arriveUserId", tmGameJudge.getScoreUserId());
			tmGameArriveMapper.updatePlayFlag(paraMap);
			//是评委才插入评委表
			if("1".equals(tmGameJudge.getJudgeFlag())){
				tmGameJudge.setTeamId(teamId);
				tmGameJudge.setTmGameId(tmGameId);
				tmGameJudge.setDeletedFlag("0");
				tmGameJudge.setCreatedBy(secUser.getSmUser().getRowId());
				tmGameJudge.setLastUpdBy(secUser.getSmUser().getRowId());
				tmGameJudge.setModificationNum(0);
				tmGameJudgeMapper.insertSelective(tmGameJudge);
			}
		}
	}
	
	public void addArriveList(Long teamId, Long tmGameId, SecurityUser secUser,List<TmGameJudge> gameJudgeList){
		synchronized(secUser){
			for(TmGameJudge tmGameJudge:gameJudgeList){
				this.deleteArriveDataByGameUser(teamId, tmGameId, tmGameJudge.getScoreUserId());
				//
				if("1".equals(tmGameJudge.getArriveFlag())){
					TmGameArrive tmGameArrive = new TmGameArrive();
					tmGameArrive.setTeamId(teamId);
					tmGameArrive.setTmGameId(tmGameId);
					tmGameArrive.setArriveUserId(tmGameJudge.getScoreUserId());
					tmGameArrive.setArriveTime(new java.util.Date());
					tmGameArrive.setSuccessFlag("1");
					tmGameArrive.setRemark("手工添加by"+secUser.getSmUser().getLogUserName()+"("+secUser.getSmUser().getLogUserCode()+")");
					tmGameArrive.setDeletedFlag("0");
					tmGameArrive.setCreatedBy(secUser.getSmUser().getRowId());
					tmGameArrive.setLastUpdBy(secUser.getSmUser().getRowId());
					tmGameArrive.setModificationNum(0);
					tmGameArriveMapper.insertSelective(tmGameArrive);
				}
				//是评委才插入评委表
				if("1".equals(tmGameJudge.getJudgeFlag())){
					tmGameJudge.setTeamId(teamId);
					tmGameJudge.setTmGameId(tmGameId);
					tmGameJudge.setDeletedFlag("0");
					tmGameJudge.setCreatedBy(secUser.getSmUser().getRowId());
					tmGameJudge.setLastUpdBy(secUser.getSmUser().getRowId());
					tmGameJudge.setModificationNum(0);
					tmGameJudgeMapper.insertSelective(tmGameJudge);
				}
			}
		}
	}
	
	public List<TmGameArrive> selectSuccessArriveRecord(Long teamId,Long tmGameId,Long userId){
		Map paraMap=new HashMap();
		paraMap.put("teamId", teamId);
		paraMap.put("tmGameId", tmGameId);
		paraMap.put("userId", userId);
		return tmGameArriveMapper.selectSuccessRecordByMap(paraMap);
	}
	//判断是否成功签到
	public boolean checkArriveRecord(Long teamId,Long tmGameId,Long userId){
		boolean bResult = false;
		List<TmGameArrive> successArriveList = selectSuccessArriveRecord(teamId, tmGameId, userId);
		if(successArriveList!=null&&successArriveList.size()>0){
			bResult = true;
		}
		return bResult;
	}
	//查询得分
	public List<TmGameScore> selectGameScore(Long teamId,Long tmGameId){
		Map paraMap = new HashMap();
		paraMap.put("teamId", teamId);paraMap.put("tmGameId", tmGameId);
		List<TmGameScore> gameScoreList = tmGameScoreMapper.selectGameScoreByMap(paraMap);
		return gameScoreList;
	}
	//删除个人签到数据
	public int deleteArriveDataByGameUser(Long teamId,Long tmGameId,Long arriveUserId){
		Map paraMap = new HashMap();
		paraMap.put("teamId", teamId);paraMap.put("tmGameId", tmGameId);paraMap.put("arriveUserId", arriveUserId);
		int iResult = tmGameArriveMapper.deleteByGameUser(paraMap);
		//删除评委数据
		paraMap.put("scoreUserId", arriveUserId);
		tmGameJudgeMapper.deleteByGameUser(paraMap);
		return iResult;
	}
	//get set
	public TmTeamGameMapper getTmTeamGameMapper() {
		return tmTeamGameMapper;
	}

	public void setTmTeamGameMapper(TmTeamGameMapper tmTeamGameMapper) {
		this.tmTeamGameMapper = tmTeamGameMapper;
	}

	public TmGameSignupMapper getTmGameSignupMapper() {
		return tmGameSignupMapper;
	}

	public void setTmGameSignupMapper(TmGameSignupMapper tmGameSignupMapper) {
		this.tmGameSignupMapper = tmGameSignupMapper;
	}
	public TmTeamPlaceMapper getTmTeamPlaceMapper() {
		return tmTeamPlaceMapper;
	}
	public void setTmTeamPlaceMapper(TmTeamPlaceMapper tmTeamPlaceMapper) {
		this.tmTeamPlaceMapper = tmTeamPlaceMapper;
	}
	public TmGameArriveMapper getTmGameArriveMapper() {
		return tmGameArriveMapper;
	}
	public void setTmGameArriveMapper(TmGameArriveMapper tmGameArriveMapper) {
		this.tmGameArriveMapper = tmGameArriveMapper;
	}
	public TmGameTechMapper getTmGameTechMapper() {
		return tmGameTechMapper;
	}
	public void setTmGameTechMapper(TmGameTechMapper tmGameTechMapper) {
		this.tmGameTechMapper = tmGameTechMapper;
	}

	public TmGameJudgeMapper getTmGameJudgeMapper() {
		return tmGameJudgeMapper;
	}

	public void setTmGameJudgeMapper(TmGameJudgeMapper tmGameJudgeMapper) {
		this.tmGameJudgeMapper = tmGameJudgeMapper;
	}

	public TmGameScoreMapper getTmGameScoreMapper() {
		return tmGameScoreMapper;
	}

	public void setTmGameScoreMapper(TmGameScoreMapper tmGameScoreMapper) {
		this.tmGameScoreMapper = tmGameScoreMapper;
	}
	public TmMemberMapper getTmMemberMapper() {
		return tmMemberMapper;
	}
	public void setTmMemberMapper(TmMemberMapper tmMemberMapper) {
		this.tmMemberMapper = tmMemberMapper;
	}
	
}
